package com.lagou.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request=  (HttpServletRequest) servletRequest;
        HttpServletResponse response=  (HttpServletResponse) servletResponse;

        // 判断用户是否登录成功，如果未登录，就跳转到登录界面
        HttpSession session = request.getSession();

        //判断session是否存在，如果存在，则判断客户是否已登陆
        Boolean isLogin = (Boolean) session.getAttribute("isLogin");

        if (isLogin == null || (!isLogin)) {
            //session不存在或者客户未登录，则跳转到登录界面
            System.out.println("--客户未登录，则重定向到index.html界面----"+isLogin);
            response.sendRedirect("/index.html");
        }else{
            System.out.println("--UserFilter--222-----"+isLogin);

            //session客户已登录，则放行
            filterChain.doFilter(servletRequest,servletResponse);
        }


    }

    @Override
    public void destroy() {

    }
}
